其他
好享学丨快速上手Python-matplotlib 箱线图绘制,学术人必备
01. 引言
02. 箱线图基本介绍
03. matplotlib绘制
import matplotlib.pyplot as plt
box_1 = data_box['box01']
box_2 = data_box['box02']
plt.rcParams['font.family'] = ["Times New Roman"]
fig, ax = plt.subplots(figsize=(6,4),dpi=200)
box_plot01 = ax.boxplot(box_1,positions =[1],patch_artist = True,widths=0.4,vert=False,
boxprops={'color':'black','facecolor':'dimgray','lw':0.7},
medianprops={'color':'black'},
capprops={'color':'black','lw':.9},
whiskerprops={'color':'black','lw':0.9},
flierprops={'lw':.8,'alpha':.7,'markersize':3.5,'marker':'d'})
box_plot02 = ax.boxplot(box_2,positions =[2],patch_artist = True,widths=0.4,vert=False,
boxprops={'color':'black','facecolor':'darkgray','lw':0.7},
medianprops={'color':'black'},
capprops={'color':'black','lw':.9},
whiskerprops={'color':'black','lw':0.9},
flierprops={'lw':.8,'alpha':.5,'markersize':3.5,'marker':'d'})
#添加图例
ax.legend([box_plot01["boxes"][0], box_plot02["boxes"][0]], ['Train sample', 'Test_sample'],
loc='upper right',frameon=False)
#设置刻度属性
ax.tick_params(left=True,bottom=True,direction='in',labelsize=12)
#设置y轴范围
ax.set_ylim(.5,2.8)
#设置y轴刻度label
ax.set_yticklabels([])
#设置x、y轴
fontdict1 = {"size":13,"color":"k",'family':'Times New Roman'}
ax.set_xlabel("Values", fontdict=fontdict1)
ax.set_ylabel("Different Type Data ", fontdict=fontdict1)
#添加序号
ax.set_title('Box plots for different types data\n',fontsize=15)
text_font = {'family':'Times New Roman','size':'22','weight':'bold','color':'black'}
ax.text(.02,.9,"(a)",transform = ax.transAxes,fontdict=text_font,zorder=4)
ax.text(.8,.056,'\nVisualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 8.5,color='black')
plt.savefig(r'E:\Data_resourses\DataCharm 公众号\Python\学术图表绘制\boxplot_region.png',width=6,height=4,
dpi=900,bbox_inches='tight')
plt.show()#显示图像
04. seaborn 绘制
plt.rcParams['font.family'] = ["Times New Roman"]
fig, ax = plt.subplots(figsize=(6,4),dpi=200)
palette = ['dimgray','darkgray']
sns_box = sns.boxplot(x=plot_data['AOD_550nm'],y=plot_data['Type'],hue=plot_data['Type'],
palette=palette,dodge=False,width=.4,ax=ax)
#去除图例title
ax.legend(frameon=False).set_title("")
ax.tick_params(left=True,bottom=True,direction='in',labelsize=12)
ax.set_yticklabels([])
ax.set_ylabel("Different Type Data ", fontdict=fontdict1)
ax.set_xlabel("Values",fontdict=fontdict1)
ax.set_title('Box plots for different types data\n',fontsize=15)
ax.text(.02,.9,"(a)",transform = ax.transAxes,fontdict=text_font,zorder=4)
ax.text(.8,.056,'\nVisualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 8.5,color='black')
plt.savefig(r'E:\Data_resourses\DataCharm 公众号\Python\学术图表绘制\boxplot_sns.png',width=6,height=4,
dpi=900,bbox_inches='tight')
彩蛋分享
悄悄告诉你,来高下制图,小白也能拥有高大上箱线图哦!快扫码加入内测群,更有制图大牛带飞 和我们一起学习进步吧~
还在为如何制作精美图表苦恼头秃么?全新推出高下制图app,不断更新的制图模板,随心更改样式,轻松一键制图。现在积极参与高下制图内测,更有机会获得正式版会员,享受轻松作图哦。更多详情请扫码进群了解。
往期推荐